(function(){

/**
 * 一个简单的Canvas对象，封装几个简单的操作，具体看源码
 * 本对象只做学习使用，并非成熟对象
 *
 * @author zany@buzheng.org
 */
function Canvas(canvasSelector) {
	var oCanvas = document.querySelector(canvasSelector);
	if (! oCanvas) {
		alert( canvasSelector + ' is nonexistent.');
		return; 
	}
	
	ctx = oCanvas.getContext('2d');
	
	if (typeof this.drawLine == 'undefined') {
		
		// 画线
		this.drawLine = function(options) {
			
			if (options.strokeStyle)
				ctx.strokeStyle = options.strokeStyle;
			
			if (options.lineWidth)
				ctx.lineWidth = options.lineWidth;
			
			ctx.beginPath();
			ctx.moveTo(options.x1, options.y1);
			ctx.lineTo(options.x2, options.y2);
			ctx.stroke();
		};
		
		// 写文字
		this.drawText = function(options) {
			if (typeof options == 'string') {
				var text = options;
				options = {};
				options.text = text;
			}
			
			if (! options.text) {
				options.text = '';
			}
			
			if (options.fillStyle)
				ctx.fillStyle    = options.fillStyle;
			
			if (options.font)
				ctx.font         = options.font;
				
			if (options.textBaseline)
				ctx.textBaseline = options.textBaseline;
			
			options.x = options.x ? options.x : 0;
			options.y = options.y ? options.y : 0;
			
			ctx.fillText  (options.text, options.x, options.y);
		};
		
		// 画矩形
		this.drawRect = function(options) {
			options.x = options.x ? options.x : 0;
			options.y = options.y ? options.y : 0;
			
			if (options.strokeStyle)
				ctx.strokeStyle = options.strokeStyle;
				
			if (options.lineWidth)
				ctx.lineWidth = options.lineWidth;
			
			ctx.strokeRect(options.x, options.y, options.width, options.height);
		};
		
		// 画圆
		this.drawArc = function(x, y, radius, options) {
			
			if (options.strokeStyle)
				ctx.strokeStyle = options.strokeStyle;

			if (options.lineWidth)
				ctx.lineWidth = options.lineWidth;
				
			ctx.beginPath();
			ctx.arc(x, y, radius, 0, Math.PI * 2, false);
			
			ctx.stroke();
		};
	}
}

window.Canvas = Canvas;
})();